<template>
  <el-dialog width="800px" title="修改订单" :visible.sync="dialogVisible" @close="handleClose">
    <!--    <div style="height: 100%;display: flex;padding: 0 20px 20px 20px;;flex-direction:column">-->
    <el-form
      ref="submitForm"
      v-loading="formLoading"
      :model="submitForm"
      :rules="submitFormRules"
      label-width="80px"
      label-position="right"
      class="submit-form"
      style="flex: 1"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="订单编号" prop="orderNo">
            <el-input v-model="submitForm.orderNo" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="订单日期" prop="orderDate">
            <el-date-picker
              v-model="submitForm.orderDate"
              style="width: 100%"
              type="date"
              placeholder="请选择订单日期"
              value-format="yyyy-MM-dd"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户名称" prop="customerId">
            <customer-query ref="customerQuery" v-model="submitForm.customerId" @choose="handleChooseCustomer" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模具" prop="moldId">
            <mold-query ref="moldQuery" v-model="submitForm.moldId" @choose="handleChooseMold" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单价" prop="unitPrice">
            <el-input v-model="submitForm.unitPrice" type="number" placeholder="请输入单价" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数量" prop="quantity">
            <el-input v-model="submitForm.quantity" type="number" placeholder="请输入数量" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="颜色" prop="color">
            <el-input v-model="submitForm.color" placeholder="请输入颜色" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="材质" prop="material">
            <el-input v-model="submitForm.material" placeholder="请输入材质" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="尺码" prop="size">
            <el-input v-model="submitForm.size" placeholder="请输入尺码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="长度" prop="length">
            <el-input v-model="submitForm.length" placeholder="请输入长度" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="优先级" prop="priority">
        <dict-select v-model="submitForm.priority" dict-key="priority" placeholder="请选择优先级" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="submitForm.remark"
          placeholder="备注"
          maxlength="100"
          show-word-limit
          type="textarea"
          :autosize="{ minRows: 2}"
        />
      </el-form-item>
    </el-form>
    <!--      <div style="display: flex">
        <el-button
          style="flex: 1"
          size="medium"
          :loading="formLoading"
          @click="handleClose"
        >取消</el-button>
        <el-button
          style="flex: 1"
          size="medium"
          :loading="formLoading"
          type="primary"
          @click="handleSubmit"
        >提交</el-button>
      </div>-->
    <!--    </div>-->
    <span slot="footer" class="dialog-footer">
      <el-button :loading="formLoading" type="text" @click="handleClose">取消</el-button>
      <el-button :loading="formLoading" type="primary" @click="handleSubmit">提交</el-button>
    </span>
    <div v-if="productAttrVisible">
      <product-attribute-module ref="productAttrModule" :visible="productAttrVisible" @close="handleProductAttrChange" />
    </div>
  </el-dialog>
</template>

<script>

import ProductAttributeModule from '@/modules/productAttributeModule.vue'
import { order_detail, order_update } from '@/api/sale/order'

export default {
  name: 'UpdateModule',
  components: { ProductAttributeModule },
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formLoading: false,
      dialogVisible: this.visible,
      productAttrVisible: false,
      submitForm: {
        id: '',
        orderNo: '',
        orderDate: '',
        customerId: '',
        moldId: '',
        unitPrice: '',
        quantity: '',
        priority: '',
        color: '',
        material: '',
        size: '',
        length: '',
        remark: ''
      },
      submitFormRules: {
        orderDate: [{ required: true, message: '请选择订单日期', trigger: 'change' }],
        customerId: [{ required: true, message: '请输入客户名称', trigger: 'change' }],
        moldId: [{ required: true, message: '请选择模具', trigger: 'change' }],
        priority: [{ required: true, message: '请选择优先级', trigger: 'change' }],
        unitPrice: [{ required: true, message: '请输入单价', trigger: 'change' }],
        quantity: [{ required: true, message: '请选择优先级', trigger: 'change' }],
        color: [{ required: true, message: '请输入颜色', trigger: 'change' }],
        material: [{ required: true, message: '请输入材质', trigger: 'change' }],
        size: [{ required: true, message: '请输入尺码', trigger: 'change' }],
        length: [{ required: true, message: '请输入长度', trigger: 'change' }]
      }
    }
  },
  methods: {
    init(id) {
      order_detail(id).then(resp => {
        if (resp.success) {
          this.submitForm = resp.data
          this.submitForm.moldId = resp.data.productInfo.moldId
          this.submitForm.moldName = resp.data.productInfo.moldName
          this.submitForm.color = resp.data.productInfo.color
          this.submitForm.material = resp.data.productInfo.material
          this.submitForm.size = resp.data.productInfo.size
          this.submitForm.length = resp.data.productInfo.length
          this.$nextTick(() => {
            this.$refs.moldQuery.init(this.submitForm.moldName)
            this.$refs.customerQuery.init(this.submitForm.customerName)
          })
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    handleSubmit() {
      this.$refs.submitForm.validate((valid) => {
        if (valid) {
          this.formLoading = true
          order_update(this.submitForm).then(resp => {
            if (resp.success) {
              this.$message.success('操作成功')
              this.$refs.submitForm.resetFields()
              this.handleClose()
            }
          }).finally(() => {
            this.formLoading = false
          })
        }
      })
    },
    handleProductAttrChange(row) {
      this.submitForm.moldId = row.moldId
      this.submitForm.moldName = row.moldName
      this.submitForm.color = row.color
      this.submitForm.material = row.material
      this.submitForm.size = row.size
      this.submitForm.length = row.length
      this.submitForm.unitPrice = row.unitPrice
      this.submitForm.quantity = row.quantity
      this.productAttrVisible = false
    },
    handleClose() {
      this.$emit('close')
    },
    handleChooseCustomer(row) {
      if (row) {
        this.submitForm.customerId = row.id
      }
    },
    handleChooseMold(row) {
      this.productTableVisible = false
      if (row) {
        this.productAttrVisible = true
        this.$nextTick(() => {
          this.$refs.productAttrModule.init(row)
        })
      }
    },
    onInput() {
      this.$forceUpdate()
    }
  }
}
</script>
<style scoped>

</style>
